#ifndef QWECHATIMAGESELECTOR_H
#define QWECHATIMAGESELECTOR_H


#include <QtCore/qurl.h>
#include <QtCore/qobject.h>
#include <QtCore/qvariant.h>
#include <QtQml/qqmlproperty.h>
#include <QtQml/qqmlparserstatus.h>
#include <QtQml/private/qqmlpropertyvalueinterceptor_p.h>
#include <QtQml/qqml.h>

class QWeChatImageSelector :
    public QObject,
    public QQmlParserStatus,
    public QQmlPropertyValueInterceptor
{
    Q_OBJECT
    Q_PROPERTY(QUrl source READ source NOTIFY sourceChanged FINAL)
    Q_PROPERTY(QUrl url READ url WRITE setUrl FINAL)
    Q_PROPERTY(QVariantList states READ states WRITE setStates FINAL)
    Q_PROPERTY(QString separator READ separator WRITE setSeparator FINAL)
    Q_PROPERTY(bool cache READ cache WRITE setCache FINAL)
    Q_INTERFACES(QQmlParserStatus QQmlPropertyValueInterceptor)
public:
    explicit QWeChatImageSelector(QObject *parent = nullptr);

    QUrl source() const;
    void setSource(const QUrl &source);

    QUrl url() const;
    void setUrl(const QUrl &url);

    QVariantList states() const;
    void setStates(const QVariantList &states);

    QString separator() const;
    void setSeparator(const QString &separator);

    bool cache() const;
    void setCache(bool cache);

    void write(const QVariant &value) Q_DECL_OVERRIDE;
    void setTarget(const QQmlProperty &property) Q_DECL_OVERRIDE;

Q_SIGNALS:
    void sourceChanged();

protected:
    void classBegin() Q_DECL_OVERRIDE;
    void componentComplete() Q_DECL_OVERRIDE;

    void setName(const QString &name);
    void setPath(const QString &path);
    void setExtensions(const QString &ext);

    QString cacheKey() const;
    void updateSource();
    bool updateActiveStates();
    int calculateScore(const QStringList &states) const;

private:
    bool m_cache = false;
    bool m_complete = false;
    QUrl m_source;
    QUrl m_url;
    QString m_path;
    QString m_name;
    QString m_ext;
    QString m_separator = QLatin1String("@");
    QVariantList m_allStates;
    QStringList m_activeStates;
    QQmlProperty m_property;
};

#endif // QWECHATIMAGESELECTOR_H
